package love.yuanmengrobot.als.system.dao.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/5/28 16:56
 */

import love.yuanmengrobot.als.system.dao.LoginDao;
import love.yuanmengrobot.als.system.javaBean.AlsUser;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.MD5;
import love.yuanmengrobot.als.utilPublic.SqlString;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginDaoImpl implements LoginDao {

    @Override
    public AlsUser selectUser( String name,String pwd) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String password = null;
        AlsUser alsUser=null;
        try {
            password = MD5.encoderByMd5(pwd);


            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlString.selectUsetSql(name, password));

//            rs = DQL.executeQuery(SqlString.selectUsetSql(name, password));//待优化

            if(rs!=null){
                if (rs.next()) {
                    alsUser = new AlsUser();
                    alsUser.setUserid(rs.getString("USERID"));
                    alsUser.setUserphone((rs.getString(2)));//phone
                    alsUser.setUseremail(rs.getString("USEREMAIL"));
                    alsUser.setUsername(name);
                    return alsUser;
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {//回收资源
            JdbcUtil.close(rs,stmt,conn);
        }

        return alsUser;
    }
}
